home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / OS / FWGraphx / Sources / FWFontLi.cpp < prev    next >
Encoding:
Text File  |  1995-11-08  |  7.9 KB  |  281 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWFontLi.cpp
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Copyright:    © 1995 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef FWFONTLI_H
  13. #include "FWFontLi.h"
  14. #endif
  15.  
  16. #ifndef FWINTSTR_H
  17. #include "FWIntStr.h"
  18. #endif
  19.  
  20. // ----- Foundation Includes -----
  21.  
  22. #ifndef FWMEMMGR_H
  23. #include "FWMemMgr.h"
  24. #endif
  25.  
  26. #ifndef FWSTRING_H
  27. #include "FWString.h"
  28. #endif
  29.  
  30. // ----- Platform Includes -----
  31.  
  32. #ifdef FW_BUILD_WIN
  33. #include <windows.h>
  34. #endif
  35.  
  36. #if defined(FW_BUILD_MAC) && !defined(__MENUS__)
  37. #include <Menus.h>
  38. #endif
  39.  
  40. #if defined(FW_BUILD_MAC) && !defined(__SCRIPT__)
  41. #include <Script.h>
  42. #endif
  43.  
  44. //========================================================================================
  45. // Runtime infos
  46. //========================================================================================
  47.  
  48. #if FW_LIB_EXPORT_PRAGMAS
  49. #pragma lib_export on
  50. #endif
  51.  
  52. #ifdef FW_BUILD_MAC
  53. #pragma segment FWGraphx_FontList
  54. #endif
  55.  
  56. //========================================================================================
  57. // CLASS FW_CFontList
  58. //========================================================================================
  59.  
  60. FW_DEFINE_CLASS_M0(FW_CFontList)
  61.  
  62. #ifdef FW_BUILD_WIN
  63. //----------------------------------------------------------------------------------------
  64. // PrivWinFillFontList
  65. //
  66. // Windows callback method to insert font names into the supplied list.
  67. //----------------------------------------------------------------------------------------
  68.  
  69. static int CALLBACK PrivWinFillFontList(const LOGFONT* lplf,
  70.                                         const TEXTMETRIC* /* lptm */,
  71.                                         DWORD /* dwFontType */,
  72.                                         LPARAM lParam)
  73. {
  74.     FW_CPrivOrderedCollection* pList = (FW_CPrivOrderedCollection *)lParam;
  75.  
  76.     // Find the face name of the font.
  77.     FW_Char* FaceName = (FW_Char *) lplf->lfFaceName;
  78.  
  79.     // Add it to the list.
  80.     FW_CIntlString* fontName = new FW_CIntlString(FaceName);
  81.     pList->AddLast(fontName);
  82.  
  83.     return TRUE;        // Keep going
  84. }
  85. #endif
  86.  
  87. //----------------------------------------------------------------------------------------
  88. //    FW_CFontList::FW_CFontList
  89. //----------------------------------------------------------------------------------------
  90.  
  91. FW_CFontList::FW_CFontList()
  92. {
  93. #ifdef FW_BUILD_MAC
  94.     const short dummyMenuID = 7777;
  95.     MenuHandle fontListHolder = ::NewMenu(dummyMenuID, "\pODF");
  96.     
  97.     if (fontListHolder == NULL)
  98.         FW_Failure(FW_xMemoryExhausted);
  99.  
  100.     short count = 0;
  101.  
  102.     FW_TRY
  103.     {
  104.         ::AppendResMenu(fontListHolder, 'FONT');
  105.         count = ::CountMItems(fontListHolder);
  106.         Str255 itemName;
  107.  
  108.         // get System script code and language code
  109.         long systemScriptCode = ::GetScriptManagerVariable(smSysScript);
  110.         long systemLanguageCode = ::GetScriptVariable(systemScriptCode, smScriptLang);
  111.         short itemField;
  112.  
  113.         for (short k = 1; k <= count; k++)
  114.         {
  115.             ::GetMenuItemText(fontListHolder, k, itemName);
  116.             ::GetItemCmd(fontListHolder, k, &itemField);    // Check cmd key equivalent code
  117.  
  118.             ODScriptCode scriptCode = (ODScriptCode) systemScriptCode;
  119.             ODLangCode languageCode = (ODLangCode) systemLanguageCode;
  120.  
  121.             if (itemField == 0x1C)
  122.                     // key equiv value of 0x1C indicates that the icon field contains a script code
  123.             {
  124.                 ::GetItemIcon(fontListHolder, k, &itemField);    // get script code from icon field
  125.                 scriptCode = (ODScriptCode) itemField;
  126.                 if (itemField == 0x00FF)     /* kludge */
  127.                     scriptCode = (ODScriptCode) systemScriptCode;
  128.                 // Get the language code from the script code
  129.                 languageCode = (ODLangCode) ::GetScriptVariable(scriptCode, smScriptLang);
  130.             }
  131.  
  132.             FW_CIntlString* fontName = FW_NEW(FW_CIntlString, 
  133.                                                 ((const FW_Char *) &itemName[1], 
  134.                                                 (FW_CharacterCount) itemName[0],
  135.                                                 scriptCode, languageCode));
  136.             fFontList.AddLast(fontName);
  137.         }
  138.         ::DisposeMenu(fontListHolder);
  139.     }
  140.     FW_CATCH_BEGIN
  141.     FW_CATCH_EVERYTHING()
  142.     {
  143.         ::DisposeMenu(fontListHolder);
  144.         FW_THROW_SAME();
  145.     }
  146.     FW_CATCH_END
  147. #endif
  148.  
  149. #ifdef FW_BUILD_WIN
  150.     HDC dc = ::GetDC(NULL);
  151.     if(dc != 0)
  152.     {
  153.         FW_TRY
  154.         {
  155.             ::EnumFontFamilies(dc, NULL, ::PrivWinFillFontList, (LPARAM) &fFontList);
  156.         }
  157.         FW_CATCH_BEGIN
  158.         FW_CATCH_EVERYTHING()
  159.         {
  160.             ::ReleaseDC(NULL, dc);
  161.             FW_THROW_SAME();
  162.         }
  163.         FW_CATCH_END
  164.         ::ReleaseDC(NULL, dc);
  165.     }
  166. #endif
  167. }
  168.  
  169. //----------------------------------------------------------------------------------------
  170. //    FW_CFontList::~FW_CFontList
  171. //----------------------------------------------------------------------------------------
  172.  
  173. FW_CFontList::~FW_CFontList()
  174. {
  175.     FW_COrderedCollectionIterator ite(&fFontList);
  176.     for (FW_CIntlString* fontName = (FW_CIntlString *) ite.First();
  177.          ite.IsNotComplete();
  178.          fontName = (FW_CIntlString *) ite.Next())
  179.     {
  180.         delete fontName;
  181.     }
  182. }
  183.  
  184. //========================================================================================
  185. // CLASS FW_CFontIterator
  186. //========================================================================================
  187.  
  188. //----------------------------------------------------------------------------------------
  189. // FW_CFontIterator::FW_CFontIterator
  190. //----------------------------------------------------------------------------------------
  191.  
  192. FW_CFontIterator::FW_CFontIterator() :
  193.     fFontList(NULL),
  194.     fDeleteTheList(TRUE),
  195.     fIterator(NULL)    
  196. {
  197.     fFontList = FW_NEW(FW_CFontList, ());
  198.     fIterator = new FW_COrderedCollectionIterator(&fFontList->fFontList);
  199.  
  200.     FW_END_CONSTRUCTOR
  201. }
  202.  
  203. //----------------------------------------------------------------------------------------
  204. // FW_CFontIterator::FW_CFontIterator
  205. //----------------------------------------------------------------------------------------
  206.  
  207. FW_CFontIterator::FW_CFontIterator(FW_CFontList* fontList) :
  208.     fFontList(fontList),
  209.     fDeleteTheList(FALSE),
  210.     fIterator(NULL)    
  211. {
  212.     FW_ASSERT(fontList != NULL);
  213.     
  214.     fIterator = new FW_COrderedCollectionIterator(&fFontList->fFontList);
  215.  
  216.     FW_END_CONSTRUCTOR
  217. }
  218.  
  219. //----------------------------------------------------------------------------------------
  220. // FW_CFontIterator::~FW_CFontIterator
  221. //----------------------------------------------------------------------------------------
  222.  
  223. FW_CFontIterator::~FW_CFontIterator()
  224. {
  225.     FW_START_DESTRUCTOR
  226.     
  227.     if (fDeleteTheList)
  228.         delete fFontList;
  229.  
  230.     delete fIterator;
  231. }
  232.  
  233. //----------------------------------------------------------------------------------------
  234. // FW_CFontIterator::First
  235. //----------------------------------------------------------------------------------------
  236.  
  237. FW_CIntlString FW_CFontIterator::First()
  238. {
  239.     FW_CIntlString *name = (FW_CIntlString *) fIterator->First();
  240.     return name ? *name : FW_CIntlString("");
  241. }
  242.  
  243. //----------------------------------------------------------------------------------------
  244. // FW_CFontIterator::Next
  245. //----------------------------------------------------------------------------------------
  246.                             
  247. FW_CIntlString FW_CFontIterator::Next()
  248. {
  249.     FW_CIntlString *name = (FW_CIntlString *) fIterator->Next();
  250.     return name ? *name : FW_CIntlString("");
  251. }
  252.  
  253. //----------------------------------------------------------------------------------------
  254. // FW_CFontIterator::Last
  255. //----------------------------------------------------------------------------------------
  256.                             
  257. FW_CIntlString FW_CFontIterator::Last()
  258. {
  259.     FW_CIntlString *name = (FW_CIntlString *) fIterator->Last();
  260.     return name ? *name : FW_CIntlString("");
  261. }
  262.  
  263. //----------------------------------------------------------------------------------------
  264. // FW_CFontIterator::Previous
  265. //----------------------------------------------------------------------------------------
  266.                             
  267. FW_CIntlString FW_CFontIterator::Previous()
  268. {
  269.     FW_CIntlString *name = (FW_CIntlString *) fIterator->Previous();
  270.     return name ? *name : FW_CIntlString("");
  271. }
  272.  
  273. //----------------------------------------------------------------------------------------
  274. // FW_CFontIterator::IsNotComplete
  275. //----------------------------------------------------------------------------------------
  276.  
  277. FW_Boolean FW_CFontIterator::IsNotComplete()
  278. {
  279.     return fIterator->IsNotComplete();
  280. }
  281.